/*
 * @Author: Jone XIn
 * @Date: 2022-01-10 21:48:40
 * @LastEditTime: 2022-01-10 23:08:23
 * @Description: Device Model
 */
'use strict'

const { DataTypes, Model, Op } = require('sequelize');
class Device extends Model { }
const { db_config } = require('../config.json');
const dbInstance = require('../db/db');
const deviceSeq = dbInstance.getSequelizeInstance(db_config);


Device.init(
    {
        ip: {
            type: DataTypes.STRING,
            allowNull: true,
        },
        m_id: {
            type: DataTypes.STRING,
            allowNull: true,
            primaryKey: true
        },
        gen_time: {
            type: DataTypes.STRING,
            allowNull: true,
        }
    },
    {
        freezeTableName: true,
        sequelize: deviceSeq,
        modelName: 'device',
        timestamps: false
    }
)


Device.addDevice = async (data) => {
    console.log(data);
    let res = await Device.bulkCreate([data], {
        updateOnDuplicate: ['ip']
    });
    return res;
}


Device.watchPathFromDevice = async (ip) => {
    const res = await Device.findAll({
        where: {
            source_ip: ip
        }
    })
    return res;
}

module.exports = {
    Device,
    deviceSeq
};